﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Sapiens.at.SharePoint.Entities;
using Sapiens.at.SharePoint.Repositories;

namespace Sapiens.at.SharePoint.Forms
{
    /// <summary>
    /// A typed implementation of <see cref="BaseForm"/> that provides properties to access the entity of the form.
    /// </summary>
    /// <typeparam name="TEntity">The entity type representing the listitem of the form</typeparam>
    public class EntityBaseForm<TEntity> : BaseForm
        where TEntity : IEntity
    {
        TEntity _entity;
        /// <summary>
        /// Gets the entity of the form
        /// </summary>
        public TEntity Entity
        {
            get
            {
                if (_entity == null)
                {
                    _entity = ListItemMapper.GetEntity<TEntity>(SPContext.Current.ListItem);
                }
                return _entity;
            }
        }

        IListRepository _repository;
        /// <summary>
        /// Gets the repository of the <see cref="Entity"/>.
        /// </summary>
        public IListRepository EntityRepository
        {
            get
            {
                if (_repository == null)
                {
                    _repository = new ListRepository(SPContext.Current.ListId, SPContext.Current.Web);
                }
                return _repository;
            }
        }
    }
}
